package com.example.xinhuazidian

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.xinhuazidian.utils.JsonFileUtil
import com.example.xinhuazidian.adapter.QianziwenAdapter
import com.example.xinhuazidian.bean.Qianziwen
import com.example.xinhuazidian.databinding.ActivityQianziwenBinding
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar

/**
 * 千字文
 */
class QianziwenActivity : AppCompatActivity() {

    private lateinit var binding: ActivityQianziwenBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityQianziwenBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initView()
    }

    private fun initView() {
        //沉浸状态栏
        ImmersionBar.with(this)
            .statusBarDarkFont(true)//状态栏字体是深色，不写默认为亮色
            .init()

        load()
    }

    @SuppressLint("SetTextI18n")
    private fun load() {
        val str = JsonFileUtil.loadFile(this, "qianziwen.json")

        val bean: Qianziwen = Gson().fromJson(str, Qianziwen::class.java)
        initAdapter(bean.spells, bean.paragraphs)

        binding.tvTitle.text = bean.title
        binding.tvAuthor.text = "${bean.tags}·${bean.author}"
    }

    private fun initAdapter(paragraphs: MutableList<String>, spells: MutableList<String>) {
        val staggeredGridLayoutManager = StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL)
        binding.rv.layoutManager = staggeredGridLayoutManager

        //加载适配器
        val qianziwenAdapter = QianziwenAdapter(paragraphs, spells)
        binding.rv.adapter = qianziwenAdapter
    }
}